package com.ruoyi.quanzi.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.quanzi.mapper.SunPostLikeMapper;
import com.ruoyi.quanzi.domain.SunPostLike;
import com.ruoyi.quanzi.service.ISunPostLikeService;

/**
 * 帖子点赞服务实现类
 * 
 * @author ruoyi
 * @date 2024-01-01
 */
@Service
public class SunPostLikeServiceImpl implements ISunPostLikeService
{
    @Autowired(required = false)
    private SunPostLikeMapper sunPostLikeMapper;

    /**
     * 查询帖子点赞
     * 
     * @param id 帖子点赞ID
     * @return 帖子点赞
     */
    @Override
    public SunPostLike selectSunPostLikeById(Long id)
    {
        if (sunPostLikeMapper != null) {
            return sunPostLikeMapper.selectSunPostLikeById(id);
        }else {
            return new SunPostLike();
        }

    }

    /**
     * 查询帖子点赞列表
     * 
     * @param sunPostLike 帖子点赞
     * @return 帖子点赞集合
     */
    @Override
    public List<SunPostLike> selectSunPostLikeList(SunPostLike sunPostLike)
    {
        if (sunPostLikeMapper != null) {
            return sunPostLikeMapper.selectSunPostLikeList(sunPostLike);
        }else{
            return new ArrayList<>();
        }
        


    }

    /**
     * 新增帖子点赞
     * 
     * @param sunPostLike 帖子点赞
     * @return 结果
     */
    @Override
    public int insertSunPostLike(SunPostLike sunPostLike)
    {
        if (sunPostLikeMapper != null) {
            sunPostLike.setCreateTime(new Date());
            return sunPostLikeMapper.insertSunPostLike(sunPostLike);
        }
        return 1;
    }

    /**
     * 修改帖子点赞
     * 
     * @param sunPostLike 帖子点赞
     * @return 结果
     */
    @Override
    public int updateSunPostLike(SunPostLike sunPostLike)
    {
        if (sunPostLikeMapper != null) {
            sunPostLike.setUpdateTime(new Date());
            return sunPostLikeMapper.updateSunPostLike(sunPostLike);
        }
        return 1;
    }

    /**
     * 批量删除帖子点赞
     * 
     * @param ids 需要删除的帖子点赞ID
     * @return 结果
     */
    @Override
    public int deleteSunPostLikeByIds(Long[] ids)
    {
        if (sunPostLikeMapper != null) {
            return sunPostLikeMapper.deleteSunPostLikeByIds(ids);
        }
        return ids.length;
    }

    /**
     * 删除帖子点赞信息
     * 
     * @param id 帖子点赞ID
     * @return 结果
     */
    @Override
    public int deleteSunPostLikeById(Long id)
    {
        if (sunPostLikeMapper != null) {
            return sunPostLikeMapper.deleteSunPostLikeById(id);
        }
        return 1;
    }

    /**
     * 用户点赞帖子
     * 
     * @param userId 用户ID
     * @param postId 帖子ID
     * @return 结果
     */
    @Override
    public int likePost(Long userId, Long postId)
    {
        if (sunPostLikeMapper != null) {
            return sunPostLikeMapper.likePost(userId, postId);
        }
        return 1;
    }

    /**
     * 用户取消点赞帖子
     * 
     * @param userId 用户ID
     * @param postId 帖子ID
     * @return 结果
     */
    @Override
    public int unlikePost(Long userId, Long postId)
    {
        if (sunPostLikeMapper != null) {
            return sunPostLikeMapper.unlikePost(userId, postId);
        }
        return 1;
    }

    /**
     * 根据帖子ID查询点赞列表
     * 
     * @param postId 帖子ID
     * @return 点赞集合
     */
    @Override
    public List<SunPostLike> selectLikesByPostId(Long postId)
    {
        if (sunPostLikeMapper != null) {
            return sunPostLikeMapper.selectLikesByPostId(postId);
        }
        
        // 临时模拟数据
        SunPostLike queryLike = new SunPostLike();
        queryLike.setPostId(postId);
        return selectSunPostLikeList(queryLike);
    }

    /**
     * 根据用户ID查询点赞列表
     * 
     * @param userId 用户ID
     * @return 点赞集合
     */
    @Override
    public List<SunPostLike> selectLikesByUserId(Long userId)
    {
        if (sunPostLikeMapper != null) {
            return sunPostLikeMapper.selectLikesByUserId(userId);
        }else{
            return new ArrayList<>();
        }
        

    }

    /**
     * 检查用户是否点赞了某个帖子
     * 
     * @param userId 用户ID
     * @param postId 帖子ID
     * @return 是否点赞
     */
    @Override
    public boolean isPostLikedByUser(Long userId, Long postId)
    {
        if (sunPostLikeMapper != null) {
            return sunPostLikeMapper.isPostLikedByUser(userId, postId) > 0;
        }
        return false;
    }

    /**
     * 获取帖子的点赞数量
     * 
     * @param postId 帖子ID
     * @return 点赞数量
     */
    @Override
    public int getLikeCountByPostId(Long postId)
    {
        if (sunPostLikeMapper != null) {
            return sunPostLikeMapper.getLikeCountByPostId(postId);
        }
        return 10; // 模拟数据
    }
}